package com.bdqn.vueboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.vueboot.config.CustomException;
import com.bdqn.vueboot.config.R;
import com.bdqn.vueboot.dto.UserDto;
import com.bdqn.vueboot.entity.SysUser;
import com.bdqn.vueboot.service.SysUserService;
import io.jsonwebtoken.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2022-12-03 09:09:36
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("sysUser")
public class SysUserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param sysUser 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<SysUser> page, SysUser sysUser) {
        return R.success(this.sysUserService.page(page, new QueryWrapper<>(sysUser)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.success(this.sysUserService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody SysUser sysUser) {
        return R.success(this.sysUserService.save(sysUser));
    }

    /**
     * 修改数据
     *
     * @param sysUser 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody SysUser sysUser) {
        return R.success(this.sysUserService.updateById(sysUser));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.success(this.sysUserService.removeByIds(idList));
    }
//    @RequestParam(value = "usrName", required = false) String usrName,
//    @RequestParam(value = "depName", required = false) String depName,
//    @RequestParam(value = "post", required = false) String post,
//    @RequestParam(value = "creationTime", required = false) String creationTime,
//    @RequestParam(value = "creationTime2", required = false) String creationTime2

    @RequestMapping("/page")
    public R page(
                  @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                  @RequestParam(value = "limit", required = false) Integer limit,
                 UserDto userDto
                  ) {
        IPage<SysUser> page=new Page<>(pageIndex,limit);
        IPage list = sysUserService.list(page, userDto);
        return R.success(list);
    }
    @PostMapping("/add")
    public R saveVue( SysUser sysUser, @RequestParam(value = "file",required = false) MultipartFile file) throws IOException {

        int add = sysUserService.add(sysUser, file);
        return R.success(add);
    }

    @RequestMapping("/deleteById")
    public R deleteById( @RequestParam(value = "usrId", required = false) Long usrId)  {

        try {
            sysUserService.deleteByUserId(usrId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("取消失败！！！");
        }
        return R.success("取消成功！");
    }
}

